<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/26
 * Time: 14:11
 */
namespace app\index\controller;

use think\Controller;

use think\Db;

use app\common\lib\JsApi;

use app\index\controller\WxUserInfo;

Class WxUser extends Controller{



    //获取微信信息接口地址
    public function wxInfo(){
        $code = input("code");
        $parent_id = input("parent_id");
        file_put_contents( "first_block.txt", "邀请parent_id3".$parent_id.PHP_EOL, FILE_APPEND);
        $appId = config("wx.appid");
        $appsecret = config("wx.appsecret");
        $wxUserInfo =  WxUserInfo::getInstance($appId,$appsecret,$code);
        $userInfo = $wxUserInfo->init();
        $wx_userinfo = Db::name("wx_users")->where("openid",$userInfo["openid"])->find();
        $wx_user["openid"] = $userInfo["openid"];
        $wx_user["nickname"] = $userInfo["nickname"];
        $wx_user["gender"] = $userInfo["sex"] == 1 ? "男" : "女";
        $wx_user["city"] = $userInfo["city"];//龙岩
        $wx_user["province"] = $userInfo["province"];//福建
        $wx_user["country"] = $userInfo["country"];//中国
        $wx_user["headimgurl"] = $userInfo["headimgurl"];//头像链接
        $wx_user["update_time"] = time();
        if(!$wx_userinfo) {
            $wx_user["create_time"] = time();
            Db::name("wx_users")->insert($wx_user);
        }else{
            if($wx_userinfo["uid"]){
                $wx_user["uid"] = $wx_userinfo["uid"];
            }
            Db::name("wx_users")->where("openid",$userInfo["openid"])->update($wx_user);
        }

        session("userinfo",$wx_user);
        if($wx_user["uid"]){
            $real_status = Db::name("user_real")->where("uid",$wx_user["uid"])->value("real_status");
            file_put_contents("real_status.txt","zhengfuzhe:".$real_status,FILE_APPEND);
            if($real_status){
                if($real_status!=2) {
                    return $this->fetch("shop/first", ["real_status" => $real_status]);
                }else{
                    return $this->fetch("/index");
                }
            }else{
                return $this->fetch("shop/first", ["real_status" => ""]);
            }
           //
        }else{

            return $this->fetch("shop/register",["parent_id"=>$parent_id]);
        }


    }


    public function register_html(){
        return $this->fetch("shop/register");
    }


    //https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx46d2655cc7128877&redirect_uri=http://ep.jingtanhaoshop.com/index/Wx_User/wxInfo&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect
    public function wxTowxInfo($parent_id=NULL){
        $appid = config('wx.appid');
        file_put_contents( "first_block.txt", "邀请parent_id4".$parent_id.PHP_EOL, FILE_APPEND);
        if($parent_id) {
            $url = "http://ep.jingtanhaoshop.com/index/Wx_User/wxInfo?parent_id=".$parent_id;
        }else{
            $url = "http://ep.jingtanhaoshop.com/index/Wx_User/wxInfo";
        }
        $redirect_uri = urlencode($url);
        $scope = "snsapi_userinfo";
        $state = "";
        $url =  "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$redirect_uri}&response_type=code&scope={$scope}&state={$state}#wechat_redirect";
        header('location:'.$url);
        //return $url;
    }

}